package com.stx.openeyes.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaeger.library.StatusBarUtil;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.stx.openeyes.MainActivity;
import com.stx.openeyes.R;
import com.stx.openeyes.adapter.MyVideoItemAdapter;
import com.stx.openeyes.dto.Response;
import com.stx.openeyes.model.Video;
import com.stx.openeyes.utils.ContextUtil;
import com.stx.openeyes.utils.HttpAdress;
import com.stx.openeyes.utils.NToast;
import com.stx.openeyes.view.fragment.CommonFindFragment;
import com.stx.openeyes.widget.LoadDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class SearchActivity extends AppCompatActivity {
    protected Context mContext;
    private Gson mGson;
    @BindView(R.id.video_item_listview)
    ListView lvHome;
    @BindView(R.id.et_searchtext_search)
    EditText editText;
    Intent getIntent;
    TextView rm1,rm2,rm3,rm4;
    LinearLayout rm;

    private MyVideoItemAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        rm1 = findViewById(R.id.rmss1);
        rm2 = findViewById(R.id.rmss2);
        rm3 = findViewById(R.id.rmss3);
        rm4 = findViewById(R.id.rmss4);
        rm = findViewById(R.id.rmss);

        StatusBarUtil.setTranslucent(this);
        ButterKnife.bind(this);
        mContext = this;
        getIntent = getIntent();
        setToolBar();
        showSoftInputFromWindow();

        setLvAdapter();

        setListener();
    }

    List<Video> listAll = new ArrayList<Video>();

    private void search() {
        String keyword = editText.getText().toString();
        if (TextUtils.isEmpty(keyword))
            return;

        LoadDialog.show(mContext);
        OkHttpUtils.get()
                .url(String.format(HttpAdress.VIDEO_SEARCH, keyword))
                .addHeader("token", ContextUtil.getCurrentUserToken())
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("===error",e.getMessage());
                        LoadDialog.dismiss(mContext);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("===json",response);
                        mGson = new Gson();
                        Response<List<Video>> videoResponse = mGson.fromJson(response, new TypeToken<Response<List<Video>>>() {}.getType());
                        listAll.addAll(videoResponse.getData());
                        myNotify();
                        LoadDialog.dismiss(mContext);
                    }
                });
    }

    public void myNotify() {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    private void setLvAdapter() {
        mAdapter = new MyVideoItemAdapter(mContext, listAll);
        lvHome.setAdapter(mAdapter);
    }

    private void setListener() {
        lvHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, VideoDetailActivity.class);
                Bundle bundle = new Bundle();
                Video data = listAll.get(position);
                bundle.putInt("id", data.getId()); // 视频id
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void setToolBar() {
        Toolbar tb = (Toolbar) findViewById(R.id.tb_my_recommend);
        setSupportActionBar(tb);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayShowTitleEnabled(false);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick({R.id.search_submit, R.id.rmss1, R.id.rmss2, R.id.rmss3, R.id.rmss4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_submit:
                listAll.clear();
                search();
                if(listAll.size() == 0){
                    rm.setVisibility(View.VISIBLE);
                }
                break;

            case R.id.rmss1:
                String content1 = rm1.getText().toString();
                editText.setText(content1);
                listAll.clear();
                search();
                rm.setVisibility(View.INVISIBLE);
                break;

            case R.id.rmss2:
                String content2 = rm2.getText().toString();
                editText.setText(content2);
                listAll.clear();
                search();
                rm.setVisibility(View.INVISIBLE);
                break;

            case R.id.rmss3:
                String content3 = rm3.getText().toString();
                editText.setText(content3);
                listAll.clear();
                search();
                rm.setVisibility(View.INVISIBLE);
                break;

            case R.id.rmss4:
                String content4 = rm4.getText().toString();
                editText.setText(content4);
                listAll.clear();
                search();
                rm.setVisibility(View.INVISIBLE);
                break;
        }


    }

    @Override
    public void onBackPressed() {
        finish();
    }

    public void showSoftInputFromWindow(){
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
